/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package org.jackysoft.entity;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;








import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.jackysoft.bean.define.EntitySchemas;

/**
 * Describes a single workflow
 * 
 * 
 */



public class WorkflowNode extends AbstractNode {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3331610649452104841L;
	// ~ Static fields/initializers
	// /////////////////////////////////////////////

	public static final String DOCTYPE = "workflow";
	public static final String PUBLICID = "-//OpenSymphony Group//DTD OSWorkflow 2.8//EN";
	public static final String SYSTEMID = "http://www.opensymphony.com/osworkflow/workflow_2_8.dtd";
	// ~ Instance fields
	// ////////////////////////////////////////////////////////

	protected GlobalConditionsNode globalConditions;
	protected GlobalActionsNode globalActions;
	protected InitialActionsNode initialActions;
	protected JoinsNode joins;
	protected RegistersNode registers;
	protected SplitsNode splits;
	protected StepsNode steps;
	protected Map<String, String> metaAttributes;
	protected TriggerFunctionsNode triggerFunctions;
	
	
	protected String refname;	
	protected String imagePath;		
	protected String content;
	// ~ Constructors
	// ///////////////////////////////////////////////////////////

	/**
	 * 
	 */
	public WorkflowNode() {
		super();
	}

	
	public WorkflowNode(String refname){
		this.refname = refname;
	}
	/**
	 * 
	 */
	public WorkflowNode(Element root) {
		super(root);

	}

	@Override
	public Element asElement() {
		Element root = elementof("workflow");
		attr(root, "refname", refname);
		addmetas(root, this.metaAttributes);
		addchild(root, this.registers);
		addchild(root, this.triggerFunctions);
		addchild(root, this.initialActions);
		addchild(root, this.globalConditions);
		addchild(root, this.globalActions);
		addchild(root, this.steps);
		addchild(root, this.joins);
		addchild(root, this.splits);
		return root;
	}

	@Override
	public String asXML() {
		Writer writer = new StringWriter();
		OutputFormat oft = OutputFormat.createPrettyPrint();
		oft.setEncoding("UTF-8");
		Document doc = DocumentHelper.createDocument();
		doc.addDocType(DOCTYPE, PUBLICID, SYSTEMID);
		doc.add(asElement());
		XMLWriter xw = new XMLWriter(writer, oft);
		try {
			xw.write(doc);
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
		}
		return writer.toString();

	}

	public Document document() {
		Document doc = DocumentHelper.createDocument();
		doc.addDocType(DOCTYPE, PUBLICID, SYSTEMID);
		Element root = asElement();
		doc.setRootElement(root);
		return doc;
	}

	
	
	public GlobalActionsNode getGlobalActions() {
		return globalActions;
	}

	
	
	public GlobalConditionsNode getGlobalConditions() {
		return globalConditions;
	}

	
	
	public InitialActionsNode getInitialActions() {
		return initialActions;
	}

	
	
	public JoinsNode getJoins() {
		return joins;
	}

	
	
	
	public Map<String, String> getMetaAttributes() {
		return metaAttributes;
	}

	public String getRefname() {
		return refname;
	}

	
	
	public RegistersNode getRegisters() {
		return registers;
	}

	
	
	public SplitsNode getSplits() {
		return splits;
	}

	
	
	public StepsNode getSteps() {
		return steps;
	}

	
	
	public TriggerFunctionsNode getTriggerFunctions() {
		return triggerFunctions;
	}

	@Override
	protected void init(Element root) {

		this.refname = stringAttr(root, "refname");
		this.metaAttributes = metas(root);
		this.globalConditions = childof(root, "global-conditions",
				GlobalConditionsNode.class);
		this.globalActions = childof(root, "global-actions",
				GlobalActionsNode.class);
		this.initialActions = childof(root, "initial-actions",
				InitialActionsNode.class);
		this.joins = childof(root, "joins", JoinsNode.class);
		this.registers = childof(root, "registers", RegistersNode.class);
		this.splits = childof(root, "splits", SplitsNode.class);
		this.steps = childof(root, "steps", StepsNode.class);
		this.triggerFunctions = childof(root, "trigger-functions",
				TriggerFunctionsNode.class);

	}

	public void setGlobalActions(GlobalActionsNode globalActions) {
		this.globalActions = globalActions;
	}

	public void setGlobalConditions(GlobalConditionsNode globalConditions) {
		this.globalConditions = globalConditions;
	}

	public void setInitialActions(InitialActionsNode initialActions) {
		this.initialActions = initialActions;
	}

	public void setJoins(JoinsNode joins) {
		this.joins = joins;
	}

	public void setMetaAttributes(Map<String, String> metaAttributes) {
		this.metaAttributes = metaAttributes;
	}

	public void setRefname(String refname) {
		this.refname = refname;
	}

	public void setRegisters(RegistersNode registers) {
		this.registers = registers;
	}

	public void setSplits(SplitsNode splits) {
		this.splits = splits;
	}

	public void setSteps(StepsNode steps) {
		this.steps = steps;
	}

	public void setTriggerFunctions(TriggerFunctionsNode triggerFunctions) {
		this.triggerFunctions = triggerFunctions;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}


	@Override
	public String tagName() {
		// TODO Auto-generated method stub
		return "workflow";
	}
	
}
